using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Domain.Entities
{
    public class Attachment
    {
        private int id;
        private int localId;
        private string fileName;
        private string filePath;
        private string fileType;
        private int fileLength;

        private Message message;

        public virtual int Id
        {
            get { return id; }
            internal set { id = value; }
        }

        public virtual int LocalId
        {
            get { return localId; }
            internal set { localId = value; }
        }

        public virtual string FileName
        {
            get { return fileName; }
            internal set { fileName = value; }
        }

        public virtual string FilePath
        {
            get { return filePath; }
            internal set { filePath = value; }
        }

        public virtual string FileType
        {
            get { return fileType; }
            internal set { fileType = value; }
        }

        public virtual int FileLength
        {
            get { return fileLength; }
            internal set { fileLength = value; }
        }

        public virtual Message Message
        {
            get { return message; }
            internal set { message = value; }
        }

        public virtual bool HasFile
        {
            get { return filePath != null; }
        }

        internal virtual void Settle(Message message, string filePathFormat)
        {
            string newFilePath = String.Format(
                filePathFormat,
                message.Id,
                localId
            );
            if (File.Exists(newFilePath))
            {
                File.Delete(newFilePath);
            }
            File.Move(filePath, newFilePath);
            filePath = newFilePath;
            this.message = message;
            message.Attachments.Add(this);
        }

        internal virtual void Remove()
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            message.Attachments.Remove(this);
        }
    }
}
